<template>
  <div class="app-container">
    <div class="app-container-table-border">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="清单类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择清单类型" style="width: 100%;" clearable
            @change="handleQuery">
            <el-option v-for="dict in dict.type.material_manifest_type" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="清单编号" prop="manifestCode">
          <el-input v-model="queryParams.manifestCode" placeholder="请输入清单编号" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="清单名称" prop="manifestName">
          <el-input v-model="queryParams.manifestName" placeholder="请输入清单名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-row :gutter="10" class="mb8 cd-pr8 top-right-btn small-line-height">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['ma:baseManifestStandard:add']">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                v-hasPermi="['ma:baseManifestStandard:edit']">修改</el-button>
            </el-col> -->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['ma:baseManifestStandard:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
              v-hasPermi="['ma:baseManifestStandard:importData']">导入</el-button>
          </el-col>
          <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>
      </el-form>

      <el-table border stripe v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="清单类型" align="center" prop="typeName" min-width="120" show-overflow-tooltip />
        <el-table-column label="清单编号" align="center" prop="manifestCode" min-width="120" show-overflow-tooltip />
        <el-table-column label="清单名称" align="center" prop="manifestName" min-width="140" show-overflow-tooltip />
        <el-table-column label="型号" align="center" prop="model" min-width="80" show-overflow-tooltip />
        <el-table-column label="规格" align="center" prop="specification" min-width="80" show-overflow-tooltip />
        <el-table-column label="单位" align="center" prop="unit" min-width="80" show-overflow-tooltip />
        <el-table-column label="数量" header-align="center" align="right" prop="manifestNumber" min-width="80"
          show-overflow-tooltip />
        <el-table-column label="单价" header-align="center" align="right" prop="manifestPrice" min-width="80"
          show-overflow-tooltip />
        <el-table-column label="金额" header-align="center" align="right" prop="manifestTotal" min-width="80"
          show-overflow-tooltip />
        <!-- <el-table-column label="调整额" align="center" prop="adjustments" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['ma:baseManifestStandard:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['ma:baseManifestStandard:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />

      <!-- 添加或修改甲供材标准材料清单对话框 -->
      <jy-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="78px">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="清单类型" prop="type">
                <el-select v-model="form.type" placeholder="请选择清单类型" style="width: 100%;" @change="ghclChange">
                  <el-option v-for="dict in dict.type.material_manifest_type" :key="dict.value" :label="dict.label"
                    :value="dict.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="清单编号" prop="manifestCode">
                <el-input v-model="form.manifestCode" placeholder="请输入清单编号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="清单名称" prop="manifestName">
                <el-input v-model="form.manifestName" placeholder="请输入清单名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="型号" prop="model">
                <el-input v-model="form.model" placeholder="请输入型号" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="规格" prop="specification">
                <el-input v-model="form.specification" placeholder="请输入规格" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单位" prop="unit">
                <el-input v-model="form.unit" placeholder="请输入单位" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="清单数量" prop="manifestNumber">
                <el-input v-model="form.manifestNumber" placeholder="请输入清单数量" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="清单单价" prop="manifestPrice">
                <el-input v-model="form.manifestPrice" placeholder="请输入清单单价" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="清单金额" prop="manifestTotal">
                <el-input v-model="form.manifestTotal" placeholder="请输入清单金额" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="调整额" prop="adjustments">
                <el-input v-model="form.adjustments" placeholder="请输入调整额" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input type="number" v-model="form.sort" placeholder="请输入排序" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </jy-dialog>
    </div>
    <!-- 用户导入对话框 -->
    <jy-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="`${upload.url}?updateSupport=${upload.updateSupport}`" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer dialog-footer-center">
        <el-button size="small" @click="upload.open = false">关闭</el-button>
        <el-button size="small" type="primary" @click="submitFileForm">确定</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBaseManifestStandard, getBaseManifestStandard, delBaseManifestStandard, addBaseManifestStandard, updateBaseManifestStandard } from "@/api/ma/baseManifestStandard";
import { getToken } from "@/utils/auth";

export default {
  name: "StandardManifest",
  dicts: ["material_manifest_type"],

  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 甲供材标准材料清单表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        manifestCode: undefined,
        manifestName: undefined,
        model: undefined,
        specification: undefined,
        unit: undefined,
        manifestNumber: undefined,
        manifestPrice: undefined,
        manifestTotal: undefined,
        adjustments: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "清单类型不能为空", trigger: "change" }
        ],
        manifestCode: [
          { required: true, message: "清单编号不能为空", trigger: "blur" }
        ],
        manifestName: [
          { required: true, message: "清单名称不能为空", trigger: "blur" }
        ],
        // model: [
        //   { required: true, message: "型号不能为空", trigger: "blur" }
        // ],
        // specification: [
        //   { required: true, message: "规格不能为空", trigger: "blur" }
        // ],
        // unit: [
        //   { required: true, message: "单位不能为空", trigger: "blur" }
        // ],
        // manifestNumber: [
        //   { required: true, message: "清单数量不能为空", trigger: "blur" }
        // ],
        // manifestPrice: [
        //   { required: true, message: "清单单价不能为空", trigger: "blur" }
        // ],
        // manifestTotal: [
        //   { required: true, message: "清单金额不能为空", trigger: "blur" }
        // ],
        // adjustments: [
        //   { required: true, message: "调整额不能为空", trigger: "blur" }
        // ],
        // sort: [
        //   { required: true, message: "排序不能为空", trigger: "blur" }
        // ],
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/ma/baseManifestStandard/importData"
      },
    };
  },

  created () {
    this.handleQuery();
  },
  methods: {
    /** 查询甲供材标准材料清单列表 */
    getList () {
      this.loading = true;
      listBaseManifestStandard(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        maStandardId: undefined,
        type: undefined,
        typeName: undefined,
        sort: undefined,
        manifestCode: undefined,
        manifestName: undefined,
        model: undefined,
        specification: undefined,
        unit: undefined,
        manifestNumber: undefined,
        manifestPrice: undefined,
        manifestTotal: undefined,
        adjustments: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.maStandardId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加甲供材标准材料清单";
    },
    ghclChange () {
      const info = this.dict.type.material_manifest_type.find(item => item.value == this.form.type);
      this.form.typeName = info.label;
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const maStandardId = row.maStandardId || this.ids;
      getBaseManifestStandard(maStandardId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改甲供材标准材料清单";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.maStandardId != null) {
            updateBaseManifestStandard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBaseManifestStandard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const maStandardIds = row.maStandardId || this.ids;
      let title = '是否确认删除选中的数据项？';
      if (row.maStandardId) {
        title = `是否确认删除甲供材标准材料清单编号为${row.manifestCode}的数据项？`;
      }
      this.$modal.confirm(title).then(() => {
        this.loading = true;
        return delBaseManifestStandard(maStandardIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导入按钮操作 */
    handleImport () {
      this.upload.title = "甲供材标准材料清单导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate () {
      this.download('ma/baseManifestStandard/importTemplate', {
      }, `标准清单导入模板_${new Date().getTime()}.xlsx`);
    },
    // 文件上传中处理
    handleFileUploadProgress (event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess (response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm () {
      this.$refs.upload.submit();
    },
  }
};
</script>
